package com.symaster.common.geom.show.node;

import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import com.symaster.common.geom.show.node.api.LeftMenu;
import com.symaster.common.geom.show.node.api.listener.UnitChangeListener;
import com.symaster.common.geom.show.node.config.MenuConfig;
import com.symaster.common.geom.show.node.enumerate.UnitEnum;
import com.symaster.common.swing.element.collapse.CollapsePanel;
import com.symaster.common.swing.util.SwingUtil;

import javax.swing.*;
import java.awt.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author yinmiao
 * @version 2023/7/4
 */
public class NodeShowElement extends JComponent {

    private JPanel root_panel;
    private JPanel root_right_panel;
    private JComboBox<String> comboBox1;
    private JPanel top_menu_panel;
    private final CollapsePanel<JComponent> rightPanel;
    private final Set<LeftMenu> leftMenus;

    public NodeShowElement() {
        setLayout(new BorderLayout());
        add(root_panel);

        leftMenus = new HashSet<>();
        rightPanel = createCollapsePanel();
        root_right_panel.setPreferredSize(new Dimension(300, 1));
        root_right_panel.add(rightPanel);
        comboBox1.addItemListener(e -> leftMenus.forEach(e2 -> {
            List<UnitChangeListener> unitChangeListeners = e2.unitChangeListener();
            if (unitChangeListeners != null) {
                unitChangeListeners.forEach(e3 -> e3.unitChangeListener(e));
            }
        }));

        SwingUtilities.invokeLater(NodeShowElement.this::initMenu);
    }

    private void initMenu() {
        top_menu_panel.setLayout(new BorderLayout());
        top_menu_panel.add(MenuConfig.topMenu.body());

        for (LeftMenu leftMenu : MenuConfig.leftMenus) {
            leftMenus.add(leftMenu);
            rightPanel.put(new JLabel(leftMenu.title()), e -> leftMenu.body());
        }

        for (UnitEnum value : UnitEnum.values()) {
            comboBox1.addItem(value.getLabel());
        }
    }

    private CollapsePanel<JComponent> createCollapsePanel() {
        return new CollapsePanel<>(0);
    }

    public static void main(String[] args) {

        Dimension dimension = new Dimension(900, 600);
        JFrame jFrame = new JFrame();
        jFrame.setPreferredSize(dimension);
        jFrame.pack();
        SwingUtil.toCenter(jFrame);
        jFrame.setContentPane(new NodeShowElement());
        jFrame.setVisible(true);

    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        root_panel = new JPanel();
        root_panel.setLayout(new BorderLayout(0, 0));
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new BorderLayout(0, 0));
        panel1.setPreferredSize(new Dimension(0, 0));
        root_panel.add(panel1, BorderLayout.NORTH);
        root_right_panel = new JPanel();
        root_right_panel.setLayout(new BorderLayout(0, 0));
        root_panel.add(root_right_panel, BorderLayout.EAST);
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new BorderLayout(0, 0));
        root_panel.add(panel2, BorderLayout.WEST);
        final JPanel panel3 = new JPanel();
        panel3.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        panel3.setPreferredSize(new Dimension(0, 10));
        root_panel.add(panel3, BorderLayout.SOUTH);
        final JPanel panel4 = new JPanel();
        panel4.setLayout(new BorderLayout(0, 0));
        root_panel.add(panel4, BorderLayout.CENTER);
        final JPanel panel5 = new JPanel();
        panel5.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
        panel5.setPreferredSize(new Dimension(0, 25));
        panel4.add(panel5, BorderLayout.SOUTH);
        comboBox1 = new JComboBox();
        panel5.add(comboBox1, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final Spacer spacer1 = new Spacer();
        panel5.add(spacer1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
        top_menu_panel = new JPanel();
        top_menu_panel.setLayout(new BorderLayout(0, 0));
        top_menu_panel.setPreferredSize(new Dimension(0, 30));
        panel4.add(top_menu_panel, BorderLayout.NORTH);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return root_panel;
    }
}
